package com.ideal.frame.institutions.controller;

import com.ideal.frame.common.dto.ResponseModel;
import com.ideal.frame.institutions.dto.InstitutionsOrganizationDto;
import com.ideal.frame.institutions.dto.OrganizationInfoDto;
import com.ideal.frame.institutions.entity.InstitutionsOrganizationTb;
import com.ideal.frame.institutions.service.InstitutionsOrganizationService;
import com.ideal.frame.sys.form.search.OrganizationSearchForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/institutions/organization")
@Api("机构信息")
public class InstitutionsOrganizationController {

    @Autowired
    private InstitutionsOrganizationService organizationService;

    @ApiOperation(value = "保存机构信息", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("saveOrganizationInfo")
    public ResponseModel saveOrgInfo(@RequestBody OrganizationInfoDto organizationInfoDto) {
        organizationService.saveOrganizationInfo(organizationInfoDto);
        return ResponseModel.success();
    }

    @ApiOperation(value = "编辑机构信息", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("editOrganizationInfo")
    public ResponseModel editOrganizationInfo(@RequestBody OrganizationInfoDto organizationInfoDto) {
        organizationService.editOrganizationInfo(organizationInfoDto);
        return ResponseModel.success();
    }

    @ApiOperation(value = "分页查询机构信息", httpMethod = "POST", produces = "application/json", response = ResponseModel.class)
    @PostMapping("getPage")
    public ResponseModel<Page<InstitutionsOrganizationTb>> getPage(@RequestBody OrganizationSearchForm searchForm) {
        return ResponseModel.success(organizationService.getPage(searchForm));
    }

    @ApiOperation(value = "查询所有机构信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getAll")
    public ResponseModel<List<InstitutionsOrganizationTb>> getAll() {
        return ResponseModel.success(organizationService.getAll());
    }

    @ApiOperation(value = "查询没有绑定的根节点和前缀的机构", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getNotBindOrg")
    public ResponseModel<List<InstitutionsOrganizationTb>> getNotBindOrg() {
        return ResponseModel.success(organizationService.getNotBindOrg());
    }

    @ApiOperation(value = "查询机构信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("getOrganizationInfo")
    public ResponseModel<InstitutionsOrganizationDto> getOrganizationInfo(@RequestParam("gnrlid") String gnrlid) {
        return ResponseModel.success(organizationService.getOrganizationInfo(gnrlid));
    }

    @ApiOperation(value = "删除机构信息", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("deleteOrganization")
    public ResponseModel<InstitutionsOrganizationTb> deleteOrganization(@RequestParam("gnrlid") String gnrlid) {
        organizationService.deleteOrganization(gnrlid);
        return ResponseModel.success();
    }

    @ApiOperation(value = "注册机构数", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("registerOrganizationCount")
    public ResponseModel registerOrganizationCount() {
        return ResponseModel.success(organizationService.registerOrganizationCount());
    }

    @ApiOperation(value = "当前节点是2级节点", httpMethod = "GET", produces = "application/json", response = ResponseModel.class)
    @GetMapping("listMainOrganization")
    public ResponseModel listMainOrganization() {
        return ResponseModel.success(organizationService.listMainOrganization());
    }
}
